package com.hk.employeemanagementsystem.web.controller;

import com.hk.employeemanagementsystem.dto.UpdateMenuRoleDto;
import com.hk.employeemanagementsystem.entity.MenuEntity;
import com.hk.employeemanagementsystem.service.MenuEntityService;
import com.hk.employeemanagementsystem.util.web.WebCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author by 何坤
 * @Classname MenuController
 * @Description TODO
 * @Date 2019/12/13 15:30
 */
@Api(value = "用户菜单的接口")
@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    private Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuEntityService menuEntityServiceImpl;

    @ApiOperation(value = "根据用户的角色查询出该角色的菜单项")
    @GetMapping("/roleMenu/{roleId}")
    private Map<String, Object> getMenuByRole(@PathVariable Integer roleId){
        Map<String, Object> tips = new HashMap<>();
        try {
            List<MenuEntity> menuEntityByRole = menuEntityServiceImpl.findMenuEntityByRoleId(roleId);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_OPERATE);
            tips.put("data", menuEntityByRole);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_OPERATE);
        }
        return tips;
    }

    @GetMapping("/menuTree/{roleId}")
    public Map<String, Object> getMenuTreeById(@PathVariable Integer roleId){
        Map<String, Object> tips = new HashMap<>();
        try {
            Map<String, Object> data = new HashMap<>();
            List<MenuEntity> menuEntity = menuEntityServiceImpl.menuTree();
            List<Integer> menusByRId = menuEntityServiceImpl.getMenusByRId(roleId);
            data.put("menuTree", menuEntity);
            data.put("mids", menusByRId);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SELECT);
            tips.put("data", data);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SELECT);
        }
        return tips;
    }


    @PutMapping("/updateMenuRole")
    public Map<String, Object> updateMenuRole(@RequestBody UpdateMenuRoleDto updateMenuRoleDto){
        Map<String, Object> tips = new HashMap<>();
        try {
            Integer rid = updateMenuRoleDto.getRid();
            Integer[] mids = updateMenuRoleDto.getMids();
            if (menuEntityServiceImpl.updateMenuRole(rid, mids) == mids.length) {
                tips.put(WebCode.SUCCESS, true);
                tips.put(WebCode.MSG, WebCode.SUCCESS_UPDATE);
            } else {
                tips.put(WebCode.SUCCESS, false);
                tips.put(WebCode.MSG, WebCode.FAILURE_UPDATE);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_UPDATE);
        }
        return tips;
    }


}
